﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Data.Common;

using Data_Objects;
using Business_Objects;
namespace Data_Objects.Implementation
{
    //Ko public -> tất cả gọi qua dataacess
    class SqlServerPhieuChiChiTietDao : IPhieuChiChiTietDao
    {
        public SqlServerPhieuChiChiTietDao()
        {
            SQLUtility.TableName = "PhieuChiChiTiet";
        }

        //Trả về đối tượng báo ăn
        private PhieuChiChiTiet MakePhieuChiChiTiet(DataRow row)
        {
            Guid _id = Guid.Parse(row["DetailID"].ToString());
            Guid _phieuChiID = Guid.Parse(row["PhieuChiID"].ToString());
            Guid _danhMucThuChiID = Guid.Parse(row["DanhMucThuChiID"].ToString());
            Guid _maNhaCC = Guid.Parse(row["MaNhaCC"].ToString());
            Guid _maNV = Guid.Parse(row["MaNV"].ToString());
            string _tKCo = row["TKCo"].ToString();
            string _tKNo = row["TKNo"].ToString();
            decimal _thanhtien = decimal.Parse(row["Thanhtien"].ToString());
            decimal _soluong = decimal.Parse(row["Soluong"].ToString());
            decimal _dongia = decimal.Parse(row["Dongia"].ToString());


         return new PhieuChiChiTiet(_id, _phieuChiID, _danhMucThuChiID, _maNhaCC, _maNV, _tKCo, _tKNo, _thanhtien, _soluong, _dongia);
        }

        //Lấy danh sách khách hàng trả về datatable
        private IList<PhieuChiChiTiet> MakePhieuChiChiTiets(DataTable dt)  
        {
            IList<PhieuChiChiTiet> list = new List<PhieuChiChiTiet>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(MakePhieuChiChiTiet(row));
            }
            return list;
        }
        private DbParameter[] Take(PhieuChiChiTiet PhieuChiChiTiet)
        {
            DbParameter[] parameters = new DbParameter[10];
            parameters[0] = new SqlParameter("@DetailID", PhieuChiChiTiet.Id);
            parameters[1] = new SqlParameter("@PhieuChiID", PhieuChiChiTiet.PhieuChiID);
            parameters[2] = new SqlParameter("@DanhMucThuChiID", PhieuChiChiTiet.DanhMucThuChiID);

            parameters[3] = new SqlParameter("@MaNhaCC", PhieuChiChiTiet.MaNhaCC);
            parameters[4] = new SqlParameter("@MaNV", PhieuChiChiTiet.MaNV);
            parameters[5] = new SqlParameter("@TKCo", PhieuChiChiTiet.TKCo);

            parameters[6] = new SqlParameter("@TKNo", PhieuChiChiTiet.TKNo);
            parameters[7] = new SqlParameter("@Thanhtien", PhieuChiChiTiet.Thanhtien);
            parameters[8] = new SqlParameter("@Soluong", PhieuChiChiTiet.Soluong);
            parameters[9] = new SqlParameter("@Dongia", PhieuChiChiTiet.Dongia);

            return parameters;
        }
        public IList<Business_Objects.PhieuChiChiTiet> GetPhieuChiChiTiet()
        {
            SQLUtility.TableName = "PhieuChiChiTiet";
            DbParameter[] param = null;
            DataTable dt = CommandProvider.GetDatatable(SQLUtility.ProceduresGetAll, param);
            return MakePhieuChiChiTiets(dt);
        }

        public Business_Objects.PhieuChiChiTiet GetPhieuChiChiTietByID(string PhieuChiChiTietID)
        {
            SQLUtility.TableName = "PhieuChiChiTiet";

            DbParameter[] parameters = new DbParameter[1];
            parameters[0] = new SqlParameter("@DetailID", PhieuChiChiTietID);
            DataRow row = CommandProvider.GetDataRow(SQLUtility.ProceduresGetByID, parameters);
            if (row == null) { return null; }
            else
            {
                return MakePhieuChiChiTiet(row);
            }
        }
        public void InsertPhieuChiChiTiet(Business_Objects.PhieuChiChiTiet PhieuChiChiTiet)
        {
            SQLUtility.TableName = "PhieuChiChiTiet";
           int newID = CommandProvider.Insert(SQLUtility.ProceduresInsert, Take(PhieuChiChiTiet), true);
        }

        public int UpdatePhieuChiChiTiet(Business_Objects.PhieuChiChiTiet PhieuChiChiTiet)
        {
            SQLUtility.TableName = "PhieuChiChiTiet";
             try { return CommandProvider.Update(SQLUtility.ProceduresUpdate, Take(PhieuChiChiTiet)); }
            catch { return -1; }
        }

        public int DeletePhieuChiChiTiet(Business_Objects.PhieuChiChiTiet PhieuChiChiTiet)
        {
            SQLUtility.TableName = "PhieuChiChiTiet";
                try { return CommandProvider.Delete(SQLUtility.ProceduresDelete, Take(PhieuChiChiTiet)); }
            catch { return -1; }
        }
    }
}
